#include "d_additem.h"
#include "ui_d_additem.h"

D_AddItem::D_AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::D_AddItem)
{
    ui->setupUi(this);

    _id=-1;
}

D_AddItem::~D_AddItem()
{
    delete ui;
}

void D_AddItem::setItem(int id, const QString &label, const QString &detail)
{
    _id=id;
    _label=label;
    _detail=detail;

    ui->btn_submit->setText("保存");
    ui->label->setText(label);
    ui->detail->setPlainText(detail);
}

void D_AddItem::on_btn_submit_clicked()
{
    QString label=ui->label->text().trimmed();
    QString detail=ui->detail->toPlainText();
    if(label.isEmpty() || detail.isEmpty())
    {
        MESSAGE_DETAIL_EMPTY
    }

    QJsonObject obj;
    if(_id > 0)
    {
        obj.insert("id",_id);
        if(label != _label)
            obj.insert("label",label);

        if(detail != _detail)
            obj.insert("detail",detail);

        if(obj.size() > 1)
            emit updateItem(obj);
    }
    else {
        obj.insert("label",label);
        obj.insert("detail",detail);
        obj.insert("addition","计划中");
        emit addItem(obj);
    }
    accept();
}
